<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 积分商城
 * @author www.dingwei.cn
 */
class Mall extends MY_Controller {
	
	/**
	 * 地址模型
	 * @var ConsumerAddressModel
	 */
	public $mAddress;
	
	/**
	 * 消费者模型
	 * @var ConsumerMallModel
	 */
	public $mMall;
	
	public function __construct(){
		parent::__construct();
		$this->loadModel('ConsumerMallModel', 'mMall','consumer');
		$this->loadModel('ConsumerAddressModel', 'mAddress','consumer');
	}
	
	/**
	 * 获取详情
	 */
	public function getInfo(){
		//表单验证
		$vConf = array(
				array('id', '礼品id', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$id = trim(post('id'));
		
		$tA = $this->mMall->getInfo($id);
		
		$tA['address'] = '';
		
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 积分商城获取列表
	 */
	public function getList(){
		//表单验证
		$vConf = array(
				array('sort', '排序类型', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		$sort = trim(post('sort'));
		$page= trim(post('page'));
		
		$tA = array(
				'info'=>array(
						'headurl'=>getImage($this->userPortrait,'xs'),
						'name'=>$this->userName,
						'integral'=>$this->userIntegral,
				),
		);
		
		$tA['list'] = $this->mMall->appGetList($sort,$page);
		
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 礼品订单
	 */
	public function order(){
		//表单验证
		$vConf = array(
				array('id', '礼品id', 'required|is_natural_no_zero'),
				array('number', '数量', 'required|is_natural_no_zero'),
				array('address_id', '地址id', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$id = trim(post('id'));
		$number = trim(post('number'));
		$addressId = trim(post('address_id'));
		
		//验证礼品
		if ( ! $tMall = $this->mMall->getInfo($id, 2)){
			$this->appReturn(0,'礼品不存在');
		}
		
		//验证地址
		if ( ! $tAddress = $this->mAddress->getInfo($addressId, $this->userId)){
			$this->appReturn(0,'地址错误');
		}
		
		//判断积分够不
		$sumIntegral = ($tMall['integral'] * $number); //消费积分
		
		if ($this->userIntegral < $sumIntegral){
			$this->appReturn(0,'积分不够');
		}
			
		//下单
		$this->db->trans_begin();
		
		$data = array(
				'code'=>getOrderCode(),
				'consumer_id'=>$this->userId,
				'type'=>1,//消费
				'integral'=>'-'.$sumIntegral,
				'status'=>1,//未处理
				'product_param'=>json_encode(array(
						'product_id'=>$tMall['id'],
						'product_name'=>$tMall['title'],
						'integral'=>$tMall['integral'],
						'number'=>$number,
						'icon'=>$tMall['imgurl'],
				),JSON_UNESCAPED_UNICODE),
				'address_param'=>json_encode(array(
						'name'=>$tAddress['consignee'],
						'address'=>$tAddress['_address'],
						'mobile'=>$tAddress['mobile'],
				),JSON_UNESCAPED_UNICODE),
				'addtime'=>time(),
		);
		
		//保存
		$tId = $this->mMall->saveLog($data);
		if ( ! $tId){
			$this->db->trans_rollback();
			$this->appReturn(0,'兑换失败');
		}
		
		//扣积分
		if ( ! $this->mUser->save(array('integral'=>$this->userIntegral - $sumIntegral),$this->userId)){
			$this->db->trans_rollback();
			$this->appReturn(0,'兑换失败');
		}
		
		//增加商品热度
		if ( ! $this->mMall->save(array('hot'=>$tMall['hot'] + 1),$id)){
			$this->db->trans_rollback();
			$this->appReturn(0,'兑换失败');
		}
		
		$this->db->trans_commit();
		$this->appReturn(1,'兑换成功');
	}
	
	/**
	 * 获取礼品订单列表
	 */
	public function getOrderList(){
		//表单验证
		$vConf = array(
				array('page', '分页', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$page = post('page');
		$status = post('status');
		
		$tA = $this->mMall->appGetLogList($this->userId,$status,$page);
		
		if ( ! $tA){
			$this->appReturn(1,'没有更多礼品订单了');
		}
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取订单的详情
	 */
	public function getOrderDetail(){
		
	}
	
	
}